#include <string.h>
#include <iostream.h>
#include <stdlib.h>

class DlinList;

class Person{
	friend class DlinList;
protected:
	char name[10];
	int age;
	Person* next;
	static Person* point;
public:
	Person(char*nm,int ag);
	~Person(){}

	virtual void print();
};

Person::Person(char* nm,int ag)
{
	strcpy(name,nm);
	age=ag;
	next=NULL;
}

void Person::print()
{
	cout<<"name:"<<name<<endl;
	cout<<"age:"<<age<<endl;
}
class Professor:public Person
{
private:
	int SpecialNo;
public:
	Professor(char* nm,int ag,int sn):Person(nm,ag)
	{SpecialNo=sn;}

	~Professor(){};

	void print();
};



void Professor::print()
{
	Person::print();
	cout<<"zhuangye "<<SpecialNo<<endl;
}

class Staff:public Person
{
private:
	char comment;
public:
	Staff(char* nm,int ag,char cm):Person(nm,ag)
	{comment=cm;}
	~Staff(){}

	void print();
};


void Staff::print()
{
	Person::print();
	cout<<"yeji "<<comment<<endl;
}

class DlinList
{
private:
	Person* head;
	int size;
public:
	DlinList():head(NULL),size(0){}
	~DlinList();

	Person* index(int pos);
	void insert(Person* p,int pos);
	void Delete(int pos);
	void print();
};

DlinList::~DlinList()
{
	Person* curr,*prev;
	curr=head;
	while(curr!=NULL)
	{
		prev=curr;
		curr=curr->next;
		delete prev;
	}
	size=0;
}

Person* DlinList::index(int pos)
{
	if(pos==-1)
		return head;
	Person* curr=head;
	int i=0;
	if(curr!=NULL&&i<pos)
	{
		curr=curr->next;
		i++;
	}
	return curr;
}

void DlinList::insert(Person* p,int pos)
{
	Person* prev=index(pos-1);

	Person::point=p;
	if(pos==0)
	{
		Person::point->next=head;
		head=Person::point;	
	}
	else
	{
		Person::point->next=prev->next;
		prev->next=Person::point;

	}
	size++;
}

void DlinList::Delete(int pos)
{
	if(size==0)
	{
		cout<<"link is empty"<<endl;
		exit(1);
	}
	Person* kill;
	Person* prev=index(pos-1);
	if(pos==0)
	{kill=prev;
	head=head->next;
	}
	else
	{
		kill=prev->next;
		prev->next=prev->next->next;
	}
	delete kill;
	size--;
}

void DlinList::print()
{
	Person* curr=head;
	while(curr!=NULL)
	{
		curr->print();
		curr=curr->next;
	}
}

Person* Person::point=NULL;

void main()
{
	DlinList personList;

	Person *p1=new Professor("zhangsan",40,2);
	Person *p2=new Professor("lisi",50,4);
	Person *p3=new Staff("wangwu",30,'A');
	Person *p4=new Staff("zhaoliu",30,'A');

	personList.insert(p1,0);
	personList.insert(p2,1);
	personList.insert(p3,2);
	personList.insert(p4,3);

	personList.Delete(0);
	personList.print();

}